package com.baozun.configurer;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;

/**
 * 聊天服务控制
 * 
 * @Title:
 * @Company:宝尊电子商务有限公司
 * @Author:万世极缘
 * @Since:2015-2-14
 * @Copyright:Copyright (c) 2015
 * @ModifyDate:2015-2-14
 * @Version:1.1.0
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfigurer implements WebSocketMessageBrokerConfigurer {
    
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> arg0) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> arg0) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void configureClientInboundChannel(ChannelRegistration arg0) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void configureClientOutboundChannel(ChannelRegistration arg0) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // 这里设置的simple broker是指可以订阅的地址，也就是服务器可以发送的地址,
        config.enableSimpleBroker("/chat");
        //用以区分是request请求还是消息请求
        config.setApplicationDestinationPrefixes("/app");
    }
    
    /**
     * 该值一定要返回true,否则启动报错
     */
    @Override
    public boolean configureMessageConverters(List<MessageConverter> arg0) {
        // TODO Auto-generated method stub
        return true;
    }
    
    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration arg0) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 添加这个Endpoint，这样在网页中就可以通过websocket连接上服务了,其中的coordination可以考虑使用TFriend中的stFriendId
        // 这样就可以将不同的人之间的聊天分开来
        registry.addEndpoint("/coordination").withSockJS();
    }
    
}
